package com.xinzhilibrary.service.impl;

import com.xinzhilibrary.dao.ICheckUserInfoDao;
import com.xinzhilibrary.dao.IGoodsInfoDao;
import com.xinzhilibrary.model.Result;
import com.xinzhilibrary.model.UserInfo;
import com.xinzhilibrary.service.CheckUserInfoService;
import com.xinzhilibrary.util.CheckJWT;
import com.xinzhilibrary.util.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class CheckUserInfoServiceImpl implements CheckUserInfoService {
    @Autowired
    private ICheckUserInfoDao iCheckUserInfoDao;
    @Autowired
    private IGoodsInfoDao iGoodsInfoDao;

    CheckJWT checkJWT = new CheckJWT();

    JWTUtil jwtUtil = new JWTUtil();

    @Override
    public Result checkUserName(String userName, String token) {
        boolean b = checkJWT.checkJwt(token);
        if (b) {
            String UserName = iCheckUserInfoDao.checkUserName(userName);
            if (UserName != null) {
                return Result.error("您输入的用户名已存在");
            } else {
                return Result.success();
            }
        } else {
            return Result.error("身份验证失效，请重新登录");
        }
    }

    @Override
    public Result userLogin(String userName, String userPassword) {
        String token = jwtUtil.token(userName);
        UserInfo userInfo = iCheckUserInfoDao.login(userName, userPassword);
        if (userInfo != null) {
            Map<String, Object> result = new HashMap<>();
            result.put("token", token);
            result.put("userInfo", userInfo);
            return Result.success(result);
        } else {
            return Result.error("登录失败");
        }
    }

    @Override
    public Result selectAllUser(String token) {
        boolean b = checkJWT.checkJwt(token);
        if (b) {
            return Result.success(iCheckUserInfoDao.selectAllUser());
        } else {
            return Result.error("身份验证失效，请重新登录");
        }
    }

    @Override
    public Result userRegister(UserInfo userInfo, String token) {
        boolean b = checkJWT.checkJwt(token);
        if (b) {
            return Result.success(iCheckUserInfoDao.userRegister(userInfo));
        } else {
            return Result.error("身份验证失效，请重新登录");
        }
    }

    @Override
    public Result addUserMoney(double money, String userName,String token) {
        boolean b = checkJWT.checkJwt(token);
        if (b) {
            if (money <= 0){
                return Result.error("充值金额必须大于0");
            }else{
                int i = iGoodsInfoDao.updateUserMoney(money, userName);
                if (i > 0) {
                    return Result.success();
                } else {
                    return Result.error("充值失败");
                }
            }
        } else {
            return Result.error("身份验证失效，请重新登录");
        }
    }

    @Override
    public Result updateUserInfo(UserInfo userInfo, String token) {
        boolean b = checkJWT.checkJwt(token);
        if (b) {
                int i = iCheckUserInfoDao.updateUserInfo(userInfo);
                if (i > 0) {
                    return Result.success();
                } else {
                    return Result.error("充值失败");
            }
        }else {
            return Result.error("身份验证失效，请重新登录");
        }
    }
}
